/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Core.Model.Data.Variables;
using Core.Model.Data.Generics;
using Core.Model.Events;
using Core.Model.Data;
using Engine;

namespace Core.Model.Ingredients.Presentable.Visibles
{
    public class Text : Visible
    {

        #region Text Enumerations
        public enum TextJustification
        {
            Start = 1,
            End,
            Centre,
            Justified
        }

        public enum TextOrientation
        {
            Vertical = 1,
            Horizontal
        }

        public enum TextStartCorner
        {
            UpperLeft = 1,
            UpperRight,
            LowerLeft,
            LowerRight
        }
        #endregion

        #region  Exchanged attributes
        private GenericColour originalTextColour;

        public GenericColour OriginalTextColour
        {
            get { return originalTextColour; }
            set { originalTextColour = value; }
        }

        private GenericColour originalBackgroundColour;

        public GenericColour OriginalBackgroundColour
        {
            get { return originalBackgroundColour; }
            set { originalBackgroundColour = value; }
        }

        private String originalFont = Engine.MHEGConstants.DefaultFont;

        public String OriginalFont
        {
            get { return originalFont; }
            set { originalFont = value; }
        }
        
        private FontAttributes originalFontAttributes;

        public FontAttributes OriginalFontAttributes
        {
            get { return originalFontAttributes; }
            set { originalFontAttributes = value; }
        }

        private int characterSet;

        public int CharacterSet
        {
            get { return characterSet; }
            set { characterSet = value; }
        }

        private TextJustification horizontalJustification = TextJustification.Start;

        public TextJustification HorizontalJustification
        {
            get { return horizontalJustification; }
            set { horizontalJustification = value; }
        }

        private TextJustification verticalJustification = TextJustification.Start;

        public TextJustification VerticalJustification
        {
            get { return verticalJustification; }
            set { verticalJustification = value; }
        }
        private TextOrientation lineOrientation = TextOrientation.Horizontal;

        public TextOrientation LineOrientation
        {
            get { return lineOrientation; }
            set { lineOrientation = value; }
        }
        private TextStartCorner startCorner = TextStartCorner.UpperLeft;

        public TextStartCorner StartCorner
        {
            get { return startCorner; }
            set { startCorner = value; }
        }
        private bool textWrapping = false;

        public bool TextWrapping
        {
            get { return textWrapping; }
            set { textWrapping = value; }
        }

        #endregion

        #region  Internal attributes

       // private String textData;

        public String TextData
        {
            get {return  Content != null ? Content.TextContent : "";}
           // private set { textData = value; }
        }

        private GenericColour textColour;

        public GenericColour TextColour
        {
            get { return textColour; }
            private set { textColour = value; }
        }
        private GenericColour backgroundColour;

        public GenericColour BackgroundColour
        {
            get { return backgroundColour; }
            private set { backgroundColour = value; }
        }
        private FontAttributes fontAttributes;

        public FontAttributes FontAttributes
        {
            get { return fontAttributes; }
            private set { fontAttributes = value; }
        }

        public override void InittInternalAttributes ()
        {
            ContentHook = ContentHook ?? Engine.MHEGEngine.Instance.ActiveApplication.TextContentHook;

            base.InittInternalAttributes();


            //if ( Content.IsText() )
            //    textData = Content.TextContent;
            //else
            //{
            //    textData = "";
            //    log.Warn( "SetInternalAttributes: Original Content is empty, settting \"\" " );
            //}


            TextColour = originalTextColour ?? Engine.MHEGEngine.Instance.ActiveApplication.OriginalTextColour;
            BackgroundColour = originalBackgroundColour ?? Engine.MHEGEngine.Instance.ActiveApplication.OriginalBackgroundColour;
            fontAttributes = originalFontAttributes ?? Engine.MHEGEngine.Instance.ActiveApplication.OriginalFontAttributes;

        }

        #endregion

        #region  Actions
        public override void SetData ( GenericOctetString newContent )
        {
            if ( newContent == null )
            {
                throw new NullReferenceException();
                log.Error( "SetData: new included content is null." );
                return;
            }

            base.SetData( newContent );

            Content.TextContent = newContent.Value;

            //if ( Content.IsText() )
              //  textData =  Content.TextContent;

            Engine.MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );
        }

        public override void SetData ( GenericContentRef newContent )
        {
            if ( newContent == null )
            {
                throw new NullReferenceException();
                log.Error( "SetData: new included content is null." );
                return;
            }
            
            base.SetData( newContent );

            //if ( Content.IsText() )
             //   textData =  Content.TextContent;

            Engine.MHEGEngine.Instance.AsyncHandle( new ContentAvailable( this ) );
        }

        public void GetTextContent ( Variable textContentVar )
        {
            if ( Content.Reference != null )
                ( ( ContentRefVariable ) textContentVar ).Value = Content.Reference.Value;
            else
                ( ( OctetStringVariable ) textContentVar ).Value =  TextData;
        }

        public void GetTextData ( OctetStringVariable textDataVar )
        {
            textDataVar.Value = TextData;
        }

        public void SetBackgroundColour ( GenericColour newBackgroundColour )
        {
            backgroundColour.Value = newBackgroundColour.Value;

            if ( RunningStatus )
                OnRedrawArea( BoxPosition, BoxSize );
        }

        public void SetTextColour ( GenericColour newTextColour )
        {
            textColour.Value = newTextColour.Value;

            if ( RunningStatus )
                OnRedrawArea( BoxPosition, BoxSize );
        }


        public void SetFontAttributes ( FontAttributes newFontAttributes )
        {
            fontAttributes = newFontAttributes;

            if ( RunningStatus )
                OnRedrawArea( BoxPosition, BoxSize );
        }

        #endregion


        public override string ToString ()
        {
            return base.ToString() + TextData ;
        }
    }
}
